#!/bin/bash
outDir="app/src/main/assets/nebulaPreset"

## umeng
umeng_key_test="6602b6d88d21b86a1847eb57"
umeng_key_formal="6433dde6ba6a5259c4343009"

## tim
tim_appid_dev="1600043611"
tim_appid_test="1600044603"
tim_appid_formal="1600043002"

#MiniAppId
mini_appid_dev="1000000000000000"
mini_appid_test="8000000000000000"
mini_appid_formal="9000000000000003"

#baseUrl
baseUrlDev="https://api-gw.dev.huangbaoche.com/capp/"
baseUrlTest="https://api-gw.test.huangbaoche.com/capp/"
baseUrlPro="https://api-gw.huangbaoche.com/capp/"

#baseWebUrl
baseWebUrlDev="https://gweb-mpa.dev.huangbaoche.com/"
baseWebUrlTest="https://gweb-mpa.test.huangbaoche.com/"
baseWebUrlPro="https://gweb-mpa.dingyuevip.com/"

#apiKey
apiSecretKeyDev="c240d0c7-9378-4b8d-b862-231f06ca0b6b"
apiSecretKeyTest="1f547d5c-5b3a-4fe2-bc50-e1539112ca60"
apiSecretKeyPro="6ce60246-0726-409c-8681-a55f384da357"

function buildApk() {
  echo "打包中..."
  if [ "$pkgType" == 1 ]; then
    gradle -p app bundleGooglePlayRelease
  else
    gradle -p app assembleCclxRelease
  fi
  echo "打包成功..."
}

function uploadApk() {
  if [ "$uploadPgyer" == "1" ]; then
    echo "开始上传..."
    if [ "$pkgType" == 1 ]; then
        sh buildEnv/pgyer_upload.sh -k c529af4a4a46fe32b66466d9eefa23a1 -d ${description} app/build/outputs/bundle/bundleGooglePlayRelease/*-release.aab
    else
        sh buildEnv/pgyer_upload.sh -k c529af4a4a46fe32b66466d9eefa23a1 -d ${description} app/build/outputs/apk/cclx/release/*-release.apk
    fi
    echo "=======================上传完成======================="
  fi
}

function initResource() {
  if [ "$env" == 8 ]; then
    curl -s https://gitee.com/zhaohbo/config/raw/master/auto-mpaas.sh | bash -s ALIPUBA0071A3091611 default DTTIRQNW ${outDir}
    echo "离线包已切换生产环境"
  elif [ "$env" == 1 ]; then
    curl -s https://gitee.com/zhaohbo/config/raw/master/auto-mpaas.sh | bash -s ALIPUBB1F5F56281652 default DTTIRQNW ${outDir}
    echo "离线包已切换测试环境"
  elif [ "$env" == 0 ]; then
    curl -s https://gitee.com/zhaohbo/config/raw/master/auto-mpaas.sh | bash -s ALIPUBC96E42A281652 default DTTIRQNW ${outDir}
    echo "离线包已切换开发环境"
  else
    echo "注意：无法识别类型，已默认最后的配置信息"
  fi
}

env=${1}
description=${2}
uploadPgyer=${3}
pkgType=${4}
echo "===========================>脚本环境>${env}:${description}:${uploadPgyer}<==========================="

#sed -i "" "s#^app.debug=.*#app.debug=true#g" appconfig.properties

if [ "$env" == 8 ]; then
#  sed -i "" "s#^yunxin.AppKey=.*#yunxin.AppKey=$nimAppKeyPro#g" appconfig.properties
#  sed -i "" "s#^umeng.ApiKey=.*#umeng.ApiKey=$umeng_key_formal#g" appconfig.properties
#  sed -i "" "s#^tim.AppId=.*#tim.AppId=$tim_appid_formal#g" appconfig.properties
#  sed -i "" "s#^app.MPAppid=.*#app.MPAppid=$mini_appid_formal#g" appconfig.properties
#  sed -i "" "s#^app.baseUrl=.*#app.baseUrl=$baseUrlPro#g" appconfig.properties
#  sed -i "" "s#^app.baseWebUrl=.*#app.baseWebUrl=$baseWebUrlPro#g" appconfig.properties
#  sed -i "" "s#^app.apiSecretKey=.*#app.apiSecretKey=$apiSecretKeyPro#g" appconfig.properties
#  sed -i "" "s#^app.debug=.*#app.debug=false#g" appconfig.properties
#  cp -f buildEnv/network_security_config_pro.xml app/src/main/res/xml/network_security_config.xml
#  cp -f buildEnv/timpush-configs-pro.json app/src/main/assets/timpush-configs.json
  cp -f buildEnv/hs_1222_pro.png entry/src/main/resources/rawfile/hs_1222.png
  cp -f buildEnv/mpaas_pro.config entry/src/main/resources/rawfile/mpaas.config
  echo "已切换生产环境"
elif [ "$env" == 1 ]; then
#  sed -i "" "s#^yunxin.AppKey=.*#yunxin.AppKey=$nimAppKeyTest#g" appconfig.properties
#  sed -i "" "s#^umeng.ApiKey=.*#umeng.ApiKey=$umeng_key_test#g" appconfig.properties
#  sed -i "" "s#^tim.AppId=.*#tim.AppId=$tim_appid_test#g" appconfig.properties
#  sed -i "" "s#^app.MPAppid=.*#app.MPAppid=$mini_appid_test#g" appconfig.properties
#  sed -i "" "s#^app.baseUrl=.*#app.baseUrl=$baseUrlTest#g" appconfig.properties
#  sed -i "" "s#^app.baseWebUrl=.*#app.baseWebUrl=$baseWebUrlTest#g" appconfig.properties
#  sed -i "" "s#^app.apiSecretKey=.*#app.apiSecretKey=$apiSecretKeyTest#g" appconfig.properties
#  sed -i "" "s#^app.debug=.*#app.debug=true#g" appconfig.properties
#  cp -f buildEnv/network_security_config_dev.xml app/src/main/res/xml/network_security_config.xml
#  cp -f buildEnv/timpush-configs-test.json app/src/main/assets/timpush-configs.json
  cp -f buildEnv/hs_1222_test.png entry/src/main/resources/rawfile/hs_1222.png
  cp -f buildEnv/mpaas_test.config entry/src/main/resources/rawfile/mpaas.config
  echo "已切换测试环境"
elif [ "$env" == 0 ]; then
#  sed -i "" "s#^yunxin.AppKey=.*#yunxin.AppKey=$nimAppKeyDev#g" appconfig.properties
#  sed -i "" "s#^umeng.ApiKey=.*#umeng.ApiKey=$umeng_key_test#g" appconfig.properties
#  sed -i "" "s#^tim.AppId=.*#tim.AppId=$tim_appid_dev#g" appconfig.properties
#  sed -i "" "s#^app.MPAppid=.*#app.MPAppid=$mini_appid_dev#g" appconfig.properties
#  sed -i "" "s#^app.baseUrl=.*#app.baseUrl=$baseUrlDev#g" appconfig.properties
#  sed -i "" "s#^app.baseWebUrl=.*#app.baseWebUrl=$baseWebUrlDev#g" appconfig.properties
#  sed -i "" "s#^app.apiSecretKey=.*#app.apiSecretKey=$apiSecretKeyDev#g" appconfig.properties
#  sed -i "" "s#^app.debug=.*#app.debug=true#g" appconfig.properties
#  cp -f buildEnv/network_security_config_dev.xml app/src/main/res/xml/network_security_config.xml
#  cp -f buildEnv/timpush-configs-dev.json app/src/main/assets/timpush-configs.json
  cp -f buildEnv/hs_1222_dev.png entry/src/main/resources/rawfile/hs_1222.png
  cp -f buildEnv/mpaas_dev.config entry/src/main/resources/rawfile/mpaas.config
  echo "已切换开发环境"
else
  echo "注意：无法识别类型，已默认最后的配置信息"
fi

#initResource

#if [ "$uploadPgyer" == 2 ]; then
#  echo ">>>环境修改已完成>>>"
#else
#  echo "start-----------package"
#  gradle clean
#
#  buildApk
#
#  uploadApk
#fi